package com.yuwj.chinaa.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/1/4.
 */

public class DateUtil {

    public static String xingq2Week(String xingq) {
        String week = "";
        switch (xingq) {
            case "星期一":
                week = "周一";
                break;
            case "星期二":
                week = "周二";
                break;
            case "星期三":
                week = "周三";
                break;
            case "星期四":
                week = "周四";
                break;
            case "星期五":
                week = "周五";
                break;
            case "星期六":
                week = "周六";
                break;
            case "星期天":
                week = "周日";
                break;
        }
        return week;
    }


    /**
     * 时间转字符串
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String formatDate(Date date, String formatter) {
        String result = null;
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(formatter);
        try {
            result = simpleFormatter.format(date);
        } catch (Exception e) {
        }
        return result;
    }

    public static String formatDate(String dateStr1, String formatter1, String formatter2) {
        String result = null;
        SimpleDateFormat simpleFormatter1 = new SimpleDateFormat(formatter1);
        SimpleDateFormat simpleFormatter2 = new SimpleDateFormat(formatter2);
        try {
            Date date = simpleFormatter1.parse(dateStr1);
            result = simpleFormatter2.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 字符串转时间
     *
     * @param dateValue
     * @param formatter
     * @return
     */
    public static Date parseDate(String dateValue, String formatter) {
        Date result = null;
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(formatter);
        try {
            result = simpleFormatter.parse(dateValue);
        } catch (Exception e) {
        }
        return result;
    }


    /**
     * 判断2个时刻是否在固定的时间间隔中，interval单位毫秒
     *
     * @param date1
     * @param date2
     * @param interval
     * @return
     */
    public static boolean ifAtSpecificInterval(Date date1, Date date2, long interval) {
        long date1Ms;
        long date2Ms;
        if (date1 != null && date2 != null) {
            date1Ms = date1.getTime();
            date2Ms = date2.getTime();
        } else {
            return false;
        }
        if (date2Ms - date1Ms <= interval && date2Ms - date1Ms > 0) {
            return true;
        } else {
            return false;
        }
    }


    public static String getTimeSpendBetwwenDate(Date date1, Date date2) {
        String spendStr = "";
        long interval = 0;
        if (date1 != null && date2 != null) {
            interval = date2.getTime() - date1.getTime();//毫秒
        } else {
            return "";
        }
        long second = interval / 1000;

        long hour = second / 3600;
        long min = second % 3600 / 60;
        long sec = second % 3600 % 60;

        if (hour == 0 && min == 0) {
            spendStr = sec + "秒";
        } else if (hour == 0) {
            spendStr = min + "分" + sec + "秒";
        } else {
            spendStr = hour + "时" + min + "分" + sec + "秒";
        }

        return spendStr;

    }


}
